import { expect } from 'chai';
import * as sinon from 'sinon';
import {appendErrorInfo, GitTools,GitOutput} from '../src/git-command-helper'
import * as step from '@flow-step/step-toolkit';
const git = new GitTools()

describe('appendErrorInfo', () => {
    let stderr: string[];
    let stdout: string[];
    let stepErrorSpy: sinon.SinonSpy;

    beforeEach(() => {
        stderr = [];
        stdout = [];
        stepErrorSpy = sinon.spy(step, 'error');
    });

    afterEach(() => {
        sinon.restore();
    });

    it('should append line to stderr and call step.error if line contains "error"', () => {
        const line = 'This is an error line';
        appendErrorInfo(line, stderr, stdout);
        expect(stderr).to.include(line);
        expect(stepErrorSpy.calledWith(line)).to.be.true;
        expect(stdout).not.to.include(line);
    });

    it('should append line to stderr and call step.error if line contains "fatal"', () => {
        const line = 'This is a fatal line';
        appendErrorInfo(line, stderr, stdout);
        expect(stderr).to.include(line);
        expect(stepErrorSpy.calledWith(line)).to.be.true;
        expect(stdout).not.to.include(line);
    });

    it('should append line to stdout if line does not contain "error" or "fatal"', () => {
        const line = 'This is a normal line';
        appendErrorInfo(line, stderr, stdout);
        expect(stdout).to.include(line);
        expect(stepErrorSpy.called).to.be.false;
        expect(stderr).not.to.include(line);
    });
});
describe('updateConfig', () => {
    let callStub: sinon.SinonStub;
    let gitOutputStub: sinon.SinonStub;

    beforeEach(() => {
        callStub = sinon.stub().resolves({ exitCode: 0 });
        gitOutputStub = sinon.stub().returns({
            exitCode: 0,
            stdout: ''
        });

        sinon.replace(require('../src/exec/call'), 'call', callStub);
    });

    afterEach(() => {
        sinon.restore();
    });

    it('should call git with correct params for default action', async () => {
        const workPath = '/path/to/workdir';
        const url1 = 'https://example.com/repo1.git';
        const url2 = 'https://example.com/repo2.git';

        const result = await git.updateConfig(undefined, workPath, url1, url2);

        expect(callStub.calledWith('git', ['config', '--add', '--global', url1, url2], workPath, sinon.match.any)).to.be.true;
        expect(result.stdout).to.equal('');
    });

    it('should call git with correct params for remove action', async () => {
        const workPath = '/path/to/workdir';
        const url1 = 'https://example.com/repo1.git';
        const url2 = 'https://example.com/repo2.git';

        const result = await git.updateConfig('remove', workPath, url1, url2);

        expect(callStub.calledWith('git', ['config', '--remove', '--global', url1, url2], workPath, sinon.match.any)).to.be.true;
        expect(result.stdout).to.equal('');
    });

    it('should handle non-zero exit code', async () => {
        callStub.resolves({ exitCode: 1 });

        const workPath = '/path/to/workdir';
        const url1 = 'https://example.com/repo1.git';
        const url2 = 'https://example.com/repo2.git';

        const result = await git.updateConfig(undefined, workPath, url1, url2);

        expect(callStub.calledWith('git', ['config', '--add', '--global', url1, url2], workPath, sinon.match.any)).to.be.true;
        expect(result.stdout).to.equal('');
    });
});